import * as faceapi from "face-api.js";

main()

async function main(){
    // 加载模型权重文件
    await faceapi.nets.ssdMobilenetv1.loadFromUri('http://127.0.0.1:8888/weights')
    await faceapi.nets.faceLandmark68Net.loadFromUri('http://127.0.0.1:8888/weights')
    await faceapi.nets.faceRecognitionNet.loadFromUri('http://127.0.0.1:8888/weights')
    await faceapi.loadFaceLandmarkModel('http://127.0.0.1:8888/weights')

    onPlay(document.getElementById('inputVideo'))

    closeLoading()
}

let drawBoxs = true
let drawLandmarks = true

async function onPlay(videoEl){
    if(!videoEl.currentTime || videoEl.paused || videoEl.ended)
        return setTimeout(()=>onPlay(videoEl))

    const options = new faceapi.SsdMobilenetv1Options({ minConfidence:0.5 })
    let results = await faceapi.detectAllFaces(videoEl, options).withFaceLandmarks()
    const canvas = document.getElementById('overlay')
    const dims = faceapi.matchDimensions(canvas, videoEl, true)

    const resizedResults = faceapi.resizeResults(results, dims)
    if(drawBoxs){
        faceapi.draw.drawDetections(canvas, resizedResults)
    }
    if(drawLandmarks){
        faceapi.draw.drawFaceLandmarks(canvas, resizedResults)
    }

    setTimeout(()=>onPlay(videoEl))
       
}